<template>
  <div class="z-switch" :class="{'is-checked':value}" @click="handleClick">
    <input type="checkbox" :name="name" class="z-switch_input" ref='checkbox'>
    <span class="z-switch_core" :style="{background,borderColor:background}">
      <span class="z-switch_button"></span>
    </span>
  </div>
</template>

<script>
export default {
  name: 'ZSwitch',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    activeColor: {
      type: String,
      default: ''
    },
    inactiveColor: {
      type: String,
      default: ''
    },
    name: {
      type: String,
      default: ''
    }
  },
  computed: {
    background () {
      return this.value ? this.activeColor : this.inactiveColor
    }
  },
  methods: {
    handleClick () {
      this.$emit('input', !this.value)
      this.$refs.checkbox.checked = !this.value
    }
  }
}
</script>

<style lang='scss' scoped>
.z-switch{
  display: inline-block;
  align-items: center;
  position: relative;
  font-size: 14px;
  line-height: 20px;
  vertical-align: middle;
  &_core{
    border-radius: 10px;
    width: 40px;
    display: inline-block;
    position: relative;
    height: 20px;
    border:1px solid #dedfe6;
    outline: none;
    box-sizing: border-box;
    background: #dedfe6;
    cursor: pointer;
    vertical-align: middle;
    transition: all .3s;
    .z-switch_button{
      position: absolute;
      top: 1px;
      left: 1px;
      border-radius: 100%;
      width: 16px;
      height: 16px;
      background-color: #fff;
      transition: all .3s;
    }
  }
}
.is-checked{
  .z-switch_core{
    background: #409eff;
    border-color: #409eff;
    transition: all .3s;
    .z-switch_button{
      transform: translateX(20px);
      transition: all .3s;
    }
  }
}
.z-switch_input{
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
  margin: 0;
}
</style>
